package MT::Log::Base;
use strict;

use base 'MT::Log';

use vars qw( $VERSION );
$VERSION = 0.13;

sub install { MT::Log->add_class($_[0]->key, $_[0]) }    # import instead?

sub new {
    my $class  = shift;
    my %params = ref $_[0] eq 'HASH' ? %{$_[0]} : @_;     # hash or hash ref
    my $log    = $class->SUPER::new(%params) or return;
    $log->class($log->key);    # seems to fix a bug in MT 3.33
    my $app = MT->instance;
    $log->ip($app->remote_ip) if $app->can('remote_ip');    # localhost if not?
    if ($app->can('blog')) {
        my $blog_id = $app->blog ? $app->blog->id : 0;
        $log->blog_id($blog_id);
    }
    if ($app->can('user') && $app->user) {
        my $user = $app->user;
        $log->author_id($user->id);
    }
    $log->level($MT::Log::INFO) unless $log->level;
    $log;
}

sub key { lc $_[0]->class_label }

sub class_label { die 'class_label was not overriden.' }

sub metadata {    # make sure length is not exceeded.
    my $self = shift;
    if (defined $_[0]) {
        my $data = shift;
        $data = substr($data, 0, 251) . "\n..." if length($data) > 255;
        $self->column('metadata', $data);
    }
    $self->column('metadata');
}

sub description {
    my $data = $_[0]->metadata or return '';
    return <<HTML;
<pre>
$data
</pre>
HTML
}

sub log       { _log(MT::Log::INFO,     @_) }
sub diag      { _log(MT::Log::DEBUG,    @_) }
sub warning   { _log(MT::Log::WARNING,  @_) }
sub security  { _log(MT::Log::SECURITY, @_) }
sub log_error { _log(MT::Log::ERROR,    @_) }

sub _log {
    my ($level, $class, $msg, $meta) = @_;
    $meta ||= {};
    $meta = {metadata => $meta} unless ref $meta eq 'HASH';
    my $log = $class->new or return;
    $log->message($msg);
    $log->level($level);
    $log->metadata($meta->{metadata});    # backwards compatability
    $log->save or MT::log($log->errstr);
}

1;

__END__

=head1 NAME

MT::Log::Base - a base class for creating better MT Activity
Log objects quickly

=head1 VERSION

0.12

=head1 SYNOPSIS

=head1 DESCRIPTION

=head1 METHODS

=head1 AUTHOR & COPYRIGHT

MT::Log::Base is Copyright 2005-2006, Timothy Appnel, Appnel
Internet Solutions, LLC. http://appnel.com/

This program is free software; you can use, modify, and
redistribute it under the same terms as Perl 5.8.x itself.

See http://www.perl.com/perl/misc/Artistic.html

=cut

